# coding: utf-8

import sys
sys.path.append('../3_data_structure')
from graph import Graph


def build_graph(word_file):
    d = {}
    g = Graph()
    wfile = open(word_file, 'r')

    for line in wfile:
        word = line.strip()    # 删除行尾的\n符号
        for i in range(len(word)):
            bucket = word[:i] + '_' + word[i+1:]
            if bucket in d:
                d[bucket].append(word)
            else:
                d[bucket] = [word]

    print(d)
    for bucket in d.keys():
        for word1 in d[bucket]:
            for word2 in d[bucket]:
                if word1 != word2:
                    g.add_edge(word1, word2)


if __name__ == "__main__":
    build_graph('words.txt')
